package api

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/smartwalle/alipay/v3"
	"order_api/global"
	"order_api/order"
	"strconv"
)

func AliPay(c *gin.Context) {

	orderSn := c.PostForm("order_sn")

	detail, err := global.OrderService.GetOrderDetail(context.Background(), &order.GetOrderDetailRequest{OrderSn: orderSn})
	if err != nil {
		return
	}

	client, err := alipay.New(global.AppConf.AppId, global.AppConf.PrivateKey, false)
	if err != nil {
		fmt.Println(err)
		return
	}

	fl := strconv.FormatFloat(float64(detail.OrderMount), 'f', 2, 64)

	var p = alipay.TradeWapPay{}
	p.NotifyURL = "http://7cc7adb1.r26.cpolar.topnotifyurl"
	p.ReturnURL = "http://127.0.0.1:8888/returnUrl"
	p.Subject = detail.OrderSn
	p.OutTradeNo = detail.OrderSn
	p.TotalAmount = fl
	p.ProductCode = "QUICK_WAP_WAY"

	url, err := client.TradeWapPay(p)
	if err != nil {
		fmt.Println(err)
	}

	// 这个 payURL 即是用于打开支付宝支付页面的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	var payURL = url.String()
	fmt.Println(payURL)
}

func NotifyURL(c *gin.Context) {
	orderSn := c.PostForm("order_sn")
	detail, err := global.OrderService.GetOrderDetail(context.Background(), &order.GetOrderDetailRequest{OrderSn: orderSn})
	if err != nil {
		return
	}
	_, err = alipay.New(global.AppConf.AppId, global.AppConf.PrivateKey, false)
	if err != nil {
		fmt.Println(err)
		return
	}

	strconv.FormatFloat(float64(detail.OrderMount), 'f', 2, 64)
}
func ReturnURL(c *gin.Context) {

}
